{include file="public/header" /}
<style>
    .layui-flow-more a {
        display: block !important;
    }
</style>
<body class="bgc" style="background-color: #F6F8FA">
<div class="header flex-center">分类</div>
<div class="flex-layout classify-nav">
    <div class="left first">
        <ul>
            {foreach name="primary" key="key" item="vo"}
            <li data-id="{$vo.id}" class="{if $vo.id == $cate_id}active{/if} primary">
                <p class="flex-center">
                    <img src="{$vo.icon}" alt="">
                </p>
                <p>{$vo.title}</p>
            </li>
            {/foreach}
        </ul>
    </div>
</div>
<div class="classify-content">
    <ul class="left second"></ul>
    <ul class="right tpl_list"></ul>
</div>
<script id="primaryTpl" type="text/html">
    {{# for(var i = 0; i < d.data.length; i++){ }}
    <li data-id="{{d.data[i].id}}" class=" {{# if(i == 0){ }}active{{# } }} switch">
        <span></span>
        <p>{{d.data[i].title}}</p>
    </li>
    {{# } }}
</script>

<script id="template" type="text/html">
    {{# for(var i = 0; i < d.data.length; i++){ }}
    <li>
        <a href="javascript:;">
            <p class="flex-center" onclick="load_href('{:url(\'product/details\')}?id={{d.data[i].id}}',2)">
                <img lay-src="{{d.data[i].icon}}" src="{{d.data[i].icon}}" alt="">
            </p>
            <div class="rgt">
                <p class="multi">{{d.data[i].title}}</p>
                <div class="flex-layout">
                    <div>
                        <p class="old-price">￥{{d.data[i].price}}</p>
                        <p class="price">￥<span>{{d.data[i].vip_price}}</span></p>
                    </div>
                    <img data-id="{{d.data[i].id}}" class="addCart" src="/static/index/img/icon49.png" alt="">
                </div>
            </div>
        </a>
    </li>
    {{# } }}
</script>
{include file="public/menu" select="2" /}
</body>
{include file="public/footer"/}
<script>
    $(function () {
        const a = $, s = a('.switch'), t = a('.tpl_list'), v = 'active', primary = a('.primary'), second = a('.second'),
            pid = a(".first").find('.active').attr('data-id'),
            api = {
                addCart: '{:url("cart/add")}',
                getList: "{:url('product/getList')}",
                getCate: "{:url('classify/getList')}",
            },
            method = {
                getProduct: function (cate_2) {
                    getFlow(1, api.getList + "?cate_2=" + cate_2);
                },
                getCate2: function (pid) {
                    a.ajax({
                        type: 'GET',
                        url: api.getCate,
                        data: {pid: pid},
                        dataType: "json",
                        success: function (data) {
                            if (data.code === 1) {
                                var tpl = document.getElementById('primaryTpl').innerHTML;
                                laytpl(tpl).render({data: data.data}, function (html) {
                                    second.html(html);
                                });
                                t.empty();
                                if (data.data.length > 0) {
                                    method.getProduct(data.data[0].id);
                                }
                            }
                        }
                    });
                }
            }
        pid ? method.getCate2(pid) : '';
        s.click(function () {
            t.empty();
            method.getProduct(a(this).attr('data-id'));
            a(this).addClass(v).addClass(v).siblings().removeClass(v)
        })
        t.on('click', '.addCart', function () {
            return common.request(api.addCart, {pid: a(this).attr('data-id'), num: 1, type: 1});
        });
        primary.click(function () {
            method.getCate2(a(this).attr('data-id'))
            a(this).addClass(v).addClass(v).siblings().removeClass(v)
        })

    })
</script>
</html>